package com.sise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.bean.CodeMsg;
import com.sise.entity.ProductCollect;
import com.sise.entity.ProductInfo;
import com.sise.mapper.ProductCollectMapper;
import com.sise.mapper.ProductInfoMapper;
import com.sise.service.IProductCollectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zj
 */
@Service
public class ProductCollectServiceImpl extends ServiceImpl<ProductCollectMapper, ProductCollect> implements IProductCollectService {

    @Resource
    private ProductCollectMapper productCollectMapper;

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Override
    public boolean addCollection(ProductCollect productCollect) {
        QueryWrapper<ProductCollect> productCollectQueryWrapper = new QueryWrapper<>();
        productCollectQueryWrapper.lambda().eq(ProductCollect::getProductId, productCollect.getProductId())
                .eq(ProductCollect::getOpenId, productCollect.getOpenId());
        Integer integer = productCollectMapper.selectCount(productCollectQueryWrapper);
        int tag = 0;
        if (integer == 0){
            tag = productCollectMapper.insert(productCollect);
        }
        return tag == 1;
    }

    @Override
    public boolean cancelCollection(ProductCollect productCollect) {
        QueryWrapper<ProductCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("product_id", productCollect.getProductId())
                .eq("open_id", productCollect.getOpenId());
        int tag = productCollectMapper.delete(queryWrapper);
        return tag == 1;
    }

    @Override
    public CodeMsg<Integer> isCollectAndCount(ProductCollect productCollect) {
        QueryWrapper<ProductCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("product_id", productCollect.getProductId())
                .eq("open_id", productCollect.getOpenId());
        ProductCollect getProductCollect = productCollectMapper.selectOne(queryWrapper);
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productCollect.getProductId());
        int count = productCollectMapper.selectCount(queryWrapper);
        if (getProductCollect != null) {
            //setMsg为1代表该用户收藏了，0代表用户没有收藏，setData存放的是有多少个用户收藏了该商品
            return new CodeMsg<Integer>().setCode(200).setMsg("1").setData(count);
        } else {
            return new CodeMsg<Integer>().setCode(400).setMsg("0").setData(count);
        }
    }

    @Override
    public List<ProductInfo> getCollectList(String openId) {
        QueryWrapper<ProductCollect> productCollectQueryWrapper = new QueryWrapper<>();
        productCollectQueryWrapper.lambda().eq(ProductCollect::getOpenId, openId);
        List<ProductCollect> productCollects = productCollectMapper.selectList(productCollectQueryWrapper);
        List<ProductInfo> productInfoList = new ArrayList<>();
        for (ProductCollect productCollect : productCollects) {
            productInfoList.add(productInfoMapper.selectById(productCollect.getProductId()));
        }
        return productInfoList;
    }
}
